/**

 *  所有页面都自己调用
 *    plus.webview.currentWebview().show()
 *  或
 *    DoShow()
 *
 *  因此统一使用OpenWebView(), 只创建页面, 不直接显示
 *  
 *  此js的所有函数必须带有root参数(即调用页相对根目录的位置),
 *  并以与该函数的功能关联性最大的页面的对应值作为默认值
 */

var RUN_MULTI_PAGES = false; // 是否允许同一功能不同参数的多页面同时开启(如多个问题)

function HaveRun(id) {
	if (plus.webview.all().indexOf(plus.webview.getWebviewById(id)) != -1)
		return true;
	return false;
}


/**
 * 打开一个webview的代理函数
 * @param {String} url
 * @param {String} id
 * @param {Object} style
 * @param {Object} extras
 */
function OpenWebView(url, id, style, extras) {
	if (HaveRun(id)) {
		plus.webview.getWebviewById(id).close('none', 0);
	}
	let wv = plus.webview.create(url, id, style, extras);
	return wv;
}

function JumpToQues(quesId, root) {
	root = root ? root : "../";
	var wvid = 'Ques';
	if (RUN_MULTI_PAGES)
		wvid = 'Ques-' + quesId;
	OpenWebView(root + 'Question/question-detail/index.html', wvid, {}, {		"quesId": quesId
	});
}

function JumpToLec(lecId, root) {
	root = root ? root : "../";
	var wvid = 'Lec';
	if (RUN_MULTI_PAGES)
		wvid = 'Lec-' + lecId;
	OpenWebView(root + 'Lecture/lecture-detail/index.html', wvid, {}, {
		"lecId": lecId,	});
}

function JumpToUserIndex(phoneNo, root) {
	root = root ? root : "../../";
	var wvid = 'UserIndex-' + phoneNo;
	OpenWebView(root + 'Mine/user-index/userinfo.html', wvid, {}, {
		"phoneNo": phoneNo,
	});
}

function JumpToPersonalAnswer(root) {
	if (HaveRun('PersonalAnswer'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/personal-center/answer.html', 'PersonalAnswer');
}

function JumpToPersonalQuestion(root) {
	if (HaveRun('PersonalQuestion'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/personal-center/question.html', 'PersonalQuestion');
}

function JumpToPersonalFollow(root) {
	if (HaveRun('PersonalFollow'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/personal-center/follow.html', 'PersonalFollow');
}

function JumpToPersonalFans(root) {
	if (HaveRun('PersonalFans'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/personal-center/fans.html', 'PersonalFans');
}

function JumpToWallet(root) {
	// root = root ? root : "../../";
	// plus.nativeUI.toast('支付功能暂时关闭');
	/*
	mui.createWindow({
		id: 'Wallet',
		url: root + 'Mine/setting/wallet.html',
	});*/
	if (HaveRun('Wallet'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/personal-center/wallet.html', 'Wallet');
}

function JumpToSetNick(lastNick, root) {
	if (HaveRun('SetNick'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/setting/nick.html', 'SetNick', {}, {
		'lastNick': lastNick
	});
}

function JumpToSetCV(lastCV, root) {
	if (HaveRun('SetCV'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/setting/CV.html', 'SetCV', {}, {
		'lastCV': lastCV
	});
}

// 注意这个是设置自己所属的学校信息, 不是单纯选择学校
// 下面那个是单纯选择学校
function JumpToSetEdu(root) {
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/setting/edu.html', 'SetEdu');
}

// 新版: 选择学校. 通过上级页面的getEduId(eduId)函数回传数据
function JumpToSelectEdu(eduId, root) {
	if (HaveRun('SelectEdu'))
		return;
	root = root ? root : "../";
	OpenWebView(root + 'Common/select-edu.html', 'SelectEdu', {}, {
		eduId: eduId,
	});
}

function JumpToSettings(root) {
	if (HaveRun('Settings')) {
		plus.webview.getWebviewById('Settings').close('none', 0);
	}
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/setting/list.html', 'Settings');
}

function JumpToAbout(root) {
	if (HaveRun('About'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Mine/setting/about.html', 'About');
}

function JumpToImgView(loc, root) {
	if (HaveRun('ImgView'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Common/img-view.html', 'ImgView', {
		scalable: true,
		background: 'transparent'
	}, {
		"loc": loc
	});
}

function JumpToNewQues(root) {
	if (HaveRun('NewQues'))
		return;
	root = root ? root : "../";
	OpenWebView(root + 'Question/question-ask/1.html', 'NewQues');
}

function JumpToNewLec(root) {
	if (HaveRun('NewLec'))
		return;
	root = root ? root : "../";
	OpenWebView(root + 'Lecture/lecture-submit/1.html', 'NewLec');
}

function JumpToQuesScore(quesId, root) {
	if (HaveRun('QuesScore'))
		return;
	root = root ? root : "../../";
	OpenWebView(root + 'Question/question-score.html', 'QuesScore', {}, {
		'quesId': quesId
	});
}

function JumpToReport(reportData, root) {
	if (HaveRun('Report'))
		return;
	root = root ? root : "../";
	plus.webview.currentWebview().opener().append(OpenWebView(root + 'Common/report.html', 'Report', {}, reportData));
}

function JumpToMenu(menuData, root) {
	if (HaveRun('PopMenu'))
		plus.webview.getWebviewById('PopMenu').close('none', 0);
	root = root ? root : "../../";
	let menu = OpenWebView(root + 'Common/menu.html', 'PopMenu', {
		background: "transparent",
	}, menuData);
	plus.webview.currentWebview().append(menu);
}